#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

int gtid;
__thread int tid = -1;
int n = 0;

int gettid() {
	return pthread_self();
}

void *foo(void *) {
	n++;
	if (n > 5)
		tid = gettid();
	gtid = gettid();
	sleep(1);
	fprintf(stderr, "tid %d, gtid %d\n", tid, gtid);
}

int main() {
	int num = 10;

	pthread_t tids[10];

	for (int i = 0; i < num; i++) {
		pthread_create(&(tids[i]), NULL, foo, NULL);
	}

	for (int i = 0; i < num; i++) {
		pthread_join((tids[i]), NULL);
	}

	return 0;
}

